package org.nf.ssm.web.controller.book;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.ResultVO;
import org.nf.ssm.domain.Book;
import org.nf.ssm.domain.BookType;
import org.nf.ssm.service.BookService;
import org.nf.ssm.service.BookTypeService;
import org.nf.ssm.web.controller.BaseController;
import org.nf.ssm.web.vo.PageVO;
import org.nf.ssm.web.vo.TypeVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhu
 * @date 2024-10-16
 */
@RestController
@RequiredArgsConstructor
public class BookController extends BaseController {

    private final BookTypeService bookTypeService;

    private final BookService bookService;

    @GetMapping("/bookType/getAllBookType")
    public PageVO getAllBookType(@RequestParam("page") Integer pageNum,@RequestParam("limit") Integer pageSize) {
        PageInfo<BookType> allBookType = bookTypeService.getAllBookType(pageNum, pageSize);
        PageVO pageVO = new PageVO();
        pageVO.setCode(200);
        pageVO.setData(allBookType);
        pageVO.setPageNum(allBookType.getPageNum());
        pageVO.setPageSize(allBookType.getPageSize());
        pageVO.setTotal(allBookType.getTotal());
        return pageVO;
    }

    @GetMapping("/bookType/getBookTypeList")
    public ResultVO getAllBookType() {
        List<BookType> bookTypeList = bookTypeService.getAllBookType();
        return success(bookTypeList);
    }

    @PostMapping("/bookType/addBookType")
    public ResultVO addBookType(@RequestBody BookType bookType) {
        bookTypeService.addBookType(bookType);
        return success();
    }

    @PostMapping("/bookType/updateBookType")
    public ResultVO updateBookType(@RequestBody BookType bookType) {
        bookTypeService.updateBookType(bookType);
        return success();
    }

    @PostMapping("/bookType/deleteBookType")
    public ResultVO deleteBookType(Integer typeId) {
        bookTypeService.deleteBookType(typeId);
        return success();
    }

    @GetMapping("/book/getAllBook")
    public PageVO getAllBook(@RequestParam("page") Integer pageNum,@RequestParam("limit") Integer pageSize) {
        PageInfo<Book> allBook = bookService.getAllBook(pageNum, pageSize);
        PageVO pageVO = new PageVO();
        pageVO.setCode(200);
        pageVO.setData(allBook);
        pageVO.setPageNum(allBook.getPageNum());
        pageVO.setPageSize(allBook.getPageSize());
        pageVO.setTotal(allBook.getTotal());
        return pageVO;
    }

    @PostMapping("/book/addBook")
    public ResultVO addBook(@RequestBody Book book) {
        bookService.addBook(book);
        return success();
    }

    @PostMapping("/book/updateBook")
    public ResultVO updateBook(@RequestBody Book book) {
        bookService.updateBook(book);
        return success();
    }

    @PostMapping("/book/deleteBook")
    public ResultVO deleteBook(Integer bookId) {
        bookService.deleteBook(bookId);
        return success();
    }

    @GetMapping("/type/stats")
    public ResultVO getTypeList() {
        List<TypeVO> typeVOList = bookService.getTypeList();
        return success(typeVOList);
    }
}
